package ods_industry_2024.gy_10.extract

import org.apache.hudi.DataSourceWriteOptions.{PARTITIONPATH_FIELD, PRECOMBINE_FIELD, RECORDKEY_FIELD}
import org.apache.hudi.QuickstartUtils.getQuickstartWriteConfigs
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.lit

import java.text.SimpleDateFormat
import java.util.logging.{Level, Logger}
import java.util.{Calendar, Properties}

object extract_count {
  def main(args: Array[String]): Unit = {
    val spark=SparkSession.builder()
      .master("local[*]")
      .appName("数据抽取汇总")
      .config("hive.exec.dynamic.partition.mode","nonstrict")
      .config("spark.serializer","org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.extensions","org.apache.spark.sql.hudi.HoodieSparkSessionExtension")
      .config("spark.sql.legacy.avro.datetimeRebaseModeInWrite","LEGACY")
      .enableHiveSupport()
      .getOrCreate()


    //  关闭日志并设置hadoop的用户
    Logger.getLogger("org").setLevel(Level.OFF)
    System.setProperty("HADOOP_USER_NAME","root")

    val connect=new Properties()
    connect.setProperty("user","root")
    connect.setProperty("password","123456")
    connect.setProperty("driver","com.mysql.jdbc.Driver")

    val day=Calendar.getInstance()
    day.add(Calendar.DATE,-1)
    val yesterday:String=new SimpleDateFormat("yyyyMMdd").format(day.getTime)


    //  创建从mysql抽取数据到hive的方法
    def to_ods(mysql_name:String,table_name:String,recordkey:String,precombinefield:String):Unit={
      spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_industry?useSSL=false",mysql_name,connect)
        .withColumn("etldate",lit(yesterday))
        .write.mode("append")
        .format("hudi")
        .options(getQuickstartWriteConfigs)
        .option(RECORDKEY_FIELD.key(),recordkey)
        .option(PRECOMBINE_FIELD.key(),precombinefield)
        .option(PARTITIONPATH_FIELD.key(),"etldate")
        .option("hoodie.table.name",table_name)
        .save(s"hdfs://192.168.40.110:9000/user/hive/warehouse/hudi_gy_ods10.db/${table_name}")

      println(s"${table_name}完成")
    }

    //  需要剔除字段的题目
    def to_ods02(mysql_name: String, table_name: String, recordkey: String, precombinefield: String): Unit = {
      spark.read.jdbc("jdbc:mysql://192.168.40.110:3306/shtd_industry?useSSL=false", mysql_name, connect)
        .drop("ProducePrgCode")
        .withColumn("etldate", lit(yesterday))
        .write.mode("append")
        .format("hudi")
        .options(getQuickstartWriteConfigs)
        .option(RECORDKEY_FIELD.key(), recordkey)
        .option(PRECOMBINE_FIELD.key(), precombinefield)
        .option(PARTITIONPATH_FIELD.key(), "etldate")
        .option("hoodie.table.name", table_name)
        .save(s"hdfs://192.168.40.110:9000/user/hive/warehouse/hudi_gy_ods10.db/${table_name}")

      println(s"${table_name}完成")
    }

    to_ods("EnvironmentData","environmentdata","EnvoId","InPutTime")
    to_ods("ChangeRecord","changerecord","ChangeID,ChangeMachineID","ChangeEndTime")
    to_ods("BaseMachine","basemachine","BaseMachineID","MachineAddDate")
    to_ods02("ProduceRecord","producerecord","ProduceRecordID,ProduceMachineID","ProduceCodeEndTime")
    to_ods("MachineData","machinedata","MachineRecordID","MachineRecordDate")






    spark.close()

  }

}
